package basicelement;

import java.io.BufferedReader;
import java.io.FileReader;

import istar.agent.IstarJadeAgent;
import istar.behaviour.AbstractIstarBehaviour;
import istar.impl.AbstractBasicResource;
import jade.core.Agent;
import jade.core.behaviours.Behaviour;
import jade.core.behaviours.OneShotBehaviour;

public class StockOption extends AbstractBasicResource {

	private boolean log = true;
		
		public StockOption() {
			super("StockOption"); 
		}

		@Override
		public Behaviour getBehaviour() {
			return new myBehaviour(this);
		}
		
		private class myBehaviour extends AbstractIstarBehaviour{

			AbstractBasicResource resource;
			private int estimatedTime = 100;
			
			public myBehaviour(AbstractBasicResource _resource){
				super(_resource);
			}

			
			@Override
			public long getEstimatedWaitingTime() {
				return estimatedTime;
			}

			@Override
			public void myAction() {
				try {
					
					BufferedReader in = new BufferedReader(new FileReader(((IstarJadeAgent)myAgent).configurationPath));
					String str, stockFile=null, stockPricesFile=null;
				    while ((str = in.readLine()) != null) {
				        if (str.startsWith("StockFile=")){
				        	stockFile = str.substring(10);
				        }
				        if (str.startsWith("StockPricesFile=")){
				        	stockPricesFile = str.substring(16);
				        	((IstarJadeAgent)myAgent).setKnowledgeObject(getElement().getExecutionNumber(), "StockPricesFile", stockPricesFile);
				        }
				    }
				    in.close();

				    
				    if (stockFile!=null){
					    Integer lowStock=-999, highStock=-999;
					    BufferedReader bfStck = new BufferedReader(new FileReader(stockFile));
					    String line = bfStck.readLine();
					    int firstSeparator = line.indexOf(";", 0);
					    int secondSeparator = line.indexOf(";", firstSeparator+1);
					    
					    String lowStockName = line.substring(firstSeparator+1, secondSeparator);
					    String highStockName = line.substring(secondSeparator+1);
					    
					    String str2;
					    while ((str2 = bfStck.readLine())!=null){
					    	line = str2;
					    }
					    bfStck.close();
					    firstSeparator = line.indexOf(";", 0);
					    secondSeparator = line.indexOf(";", firstSeparator+1);
					    int thirdSeparator = line.indexOf(";", secondSeparator+1);
					    int fourthSeparator = line.indexOf(";", thirdSeparator+1);

					    
					    //System.out.println("firstSeparator="+firstSeparator+ " secondSeparator="+secondSeparator);
					    
					    lowStock = new Integer(line.substring(firstSeparator+1, secondSeparator));
					    highStock = new Integer(line.substring(secondSeparator+1, thirdSeparator));
					    
					    Double lowStockPriceOrdered = null;
					    Double highStockPriceOrdered = null;
					    try {
					    	lowStockPriceOrdered = new Double(line.substring(thirdSeparator+1, fourthSeparator));
						} catch (Exception e) {
							// TODO: handle exception
						}
						
						try {
							highStockPriceOrdered = new Double(line.substring(fourthSeparator+1));
						} catch (Exception e) {
							// TODO: handle exception
						}
					    
					    System.out.println("========");
					    System.out.println("LowStockQtt="+lowStock);
					    System.out.println("HighStockQtt="+lowStock);
					    System.out.println("LowStockPrice="+ lowStockPriceOrdered);
					    System.out.println("HighStockPrice="+ highStockPriceOrdered);
					    System.out.println("========");
					    
					    ((IstarJadeAgent)myAgent).setKnowledgeObject(getElement().getExecutionNumber(), "LowStockName", lowStockName);
					    ((IstarJadeAgent)myAgent).setKnowledgeObject(getElement().getExecutionNumber(), "HighStockName", highStockName);
					    ((IstarJadeAgent)myAgent).setKnowledgeObject(getElement().getExecutionNumber(), "LowStockQtt", lowStock);
					    ((IstarJadeAgent)myAgent).setKnowledgeObject(getElement().getExecutionNumber(), "HighStockQtt", highStock);
					    ((IstarJadeAgent)myAgent).setKnowledgeObject(getElement().getExecutionNumber(), "LowStockPriceOrdered", lowStockPriceOrdered);
					    ((IstarJadeAgent)myAgent).setKnowledgeObject(getElement().getExecutionNumber(), "HighStockPriceOrdered", highStockPriceOrdered);
	    
				    }
				    
				    
				    
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public int myOnEnd() {
				// TODO Auto-generated method stub
				return 1;
			}

			@Override
			public boolean done() {
				return true;
			}
		}
}

